package create.singleton.singleton_demo1;

/**
 * 双检锁,线程安全,懒加载,效率比较高
 * <p>
 * volatile修饰后1、对所有线程都具有可见性。2、禁止指令重排序优化
 * 指令重排：jvm执行java文件编译后的字节码文件，编译器优化程序性能会重新对字节码重新排序，不影响执行结果
 * volatile应用于由于指令重排导致的线程安全问题
 *
 * @author yeyangtao
 * @date 2020/5/19 17:38
 */
public class Single4 {
    private String name;
    private Integer age;

    private volatile static Single4 instance;

    private Single4() {
    }

    public static Single4 getInstance() {
        synchronized (Single4.class) {
            if (instance == null) {
                instance = new Single4();
            }
            return instance;
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
